<template>
  <div id="couponTop">
    <h1 class="con-right-title">
      优惠券管理
    </h1>
    <div class="search-box">
      <el-form
        ref="ruleForm"
        :inline="true"
        :model="formInline"
        class="demo-form-inline"
      >
        <!-- 输入搜索部分 -->
        <el-form-item label="优惠券名称" prop="couponName">
          <el-input
            v-model="formInline.couponName"
            placeholder="请输入优惠券名称"
            size="small"
            class="numberInput"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="发放类型" prop="grantType" class="search-box-select">
          <el-select
            class="short-input"
            v-model="formInline.grantType"
            placeholder="请选择发放类型"
            clearable
          >
            <el-option
              v-for="item in grantTypeList"
              :key="item.dictLabel"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关联项目" prop="projectNameId" class="search-box-select">
          <el-select
            class="short-input"
            v-model="formInline.projectNameId"
            placeholder="请选择关联项目"
            clearable
          >
            <el-option
              v-for="item in checkProjects"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="添加时间" prop="createTime">
          <el-date-picker
            v-model="formInline.createTime"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发放人" prop="userId">
          <el-select
            v-model="formInline.userId"
            filterable
            clearable
            placeholder="请输入关键词"
            class="long"
          >
            <el-option
              v-for="item in operatorsList"
              :key="item.userId"
              :label="item.nickname"
              :value="item.userId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="s-btn">
          <el-button
            type="success"
            icon="el-icon-search"
            @click="toSearch('ruleForm')"
          >
            查询
          </el-button>
          <el-button type="success" plain @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { sysNormalDisable } from '@utils/dictionariesFunc'
import { getDpNum } from '@/api/system/user'
import { getProject } from '@/api/couponsIntegral/project' // 接口
export default {
  name: 'couponTop',
  msg: '优惠券管理-顶部搜索',
  props: {
    formInline: {
      type: Object,
      default: function() {
        return {
          couponName: '', // 优惠券名称
          grantType: '', // 发放类型
          projectNameId: '', //项目标签
          userId: '', // 发放人
          startTime: '', // 开始时间
          endTime: '', // 结束时间
          createTime: []
        }
      }
    }
  },
  data() {
    return {
      grantTypeList: [{ dictLabel: '全部', dictValue: '' }], //发放类型
      checkProjects: [{ id: '全部', name: '' }], //关联项目
      // 修改人列表
      operatorsList: [{ nickname: '全部', userId: '' }]
    }
  },
  watch: {
    'formInline.createTime': {
      handler(newVal, oldVal) {
        if (newVal && newVal.length > 1) {
          this.formInline.startTime = newVal[0]
          this.formInline.endTime = newVal[1]
        } else {
          this.formInline.startTime = ''
          this.formInline.endTime = ''
        }
      },
      deep: true
    }
  },
  mounted() {
    this.getOperators()
    this.getStatusList()
    this.getProjects()
  },
  methods: {
    // 获取操作人员
    getOperators() {
      getDpNum({ type: 'OPERATORS' }, (response) => {
        this.operatorsList = this.operatorsList.concat(response)
      })
    },
    async getStatusList() {
      /*优惠券发放类型*/
      let data = await sysNormalDisable('grant_types', 'grant_types')
      this.grantTypeList = this.grantTypeList.concat(data)
      /*优惠券类型*/
      await sysNormalDisable('coupon_types', 'coupon_types')
    },
    /* 获取选中项目标签列表 */
    getProjects() {
      this.loading = true
      getProject(
        (response) => {
          this.loading = false
          this.checkProjects = response
        },
        (response) => {
          this.loading = false
        }
      )
    },
    /* 搜索 */
    toSearch(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('toSearch')
        } else {
          return false
        }
      })
    },
    /* 重置 */
    reset() {
      this.$refs['ruleForm'].resetFields()
    }
  }
}
</script>

<style lang="less">
#couponTop {
  .search-box{
    .search-box-select{
     .el-input .el-input__inner {
      padding-left: 5px;
    }
    }
  }
}
</style>
